<?php

/**
 * itineraryReservation actions.
 *
 * @package    sf_sandbox
 * @subpackage itineraryReservation
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class adminItineraryReservationActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      $this->status   = $request->getParameter('status');
      $this->ref_code = trim($request->getParameter('ref_code'));
      $this->flights  = $request->getParameter('flights');
      $this->ticket_code  = trim($request->getParameter('ticket_code'));

      $q = Doctrine_Manager::getInstance()->connection(); 

      $sql = 'SELECT R.* from aoi_itinerary_reservation R '.
          'INNER JOIN aoi_itinerary_reservation_leg RL ON RL.reservation_ID = R.id '.
          'INNER JOIN aoi_leg L ON L.id = RL.leg_id '.
          'INNER JOIN aoi_passenger P ON P.reservation_id = R.id '.
          'WHERE R.id > 0 '; // dummy WHERE for next "AND WHERE"

      if(in_array($this->status, array('created','pending','validated','cancelled'))) $sql .= ' AND R.status = \''. $this->status.'\' ';
      if($this->ref_code != '') $sql .= ' AND R.ref_code LIKE \'%'. $this->ref_code.'%\' ';
      if($this->ticket_code != '') $sql .= ' AND P.ticket_code LIKE \'%'. $this->ticket_code.'%\' ';

      if($this->flights == '') $sql .= ' AND L.actual_departure_time > NOW() ';
      elseif($this->flights == 'past') $sql .= ' AND L.actual_departure_time < NOW() ';
      elseif($this->flights == 'all') $sql .= ' ';

      $sql .= 'GROUP BY R.id ORDER BY R.created_at DESC';

      $this->reservations = $q->execute($sql); 

  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new AoiItineraryReservationForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new AoiItineraryReservationForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($aoi_itinerary_reservation = Doctrine_Core::getTable('AoiItineraryReservation')->find(array($request->getParameter('id'))), sprintf('Object aoi_itinerary_reservation does not exist (%s).', $request->getParameter('id')));
    $this->form = new AoiItineraryReservationForm($aoi_itinerary_reservation);
    $this->reservation = $aoi_itinerary_reservation;
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($aoi_itinerary_reservation = Doctrine_Core::getTable('AoiItineraryReservation')->find(array($request->getParameter('id'))), sprintf('Object aoi_itinerary_reservation does not exist (%s).', $request->getParameter('id')));
    $this->form = new AoiItineraryReservationForm($aoi_itinerary_reservation);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($aoi_itinerary_reservation = Doctrine_Core::getTable('AoiItineraryReservation')->find(array($request->getParameter('id'))), sprintf('Object aoi_itinerary_reservation does not exist (%s).', $request->getParameter('id')));
    $aoi_itinerary_reservation->delete();

    $this->redirect('itineraryReservation/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $aoi_itinerary_reservation = $form->save();

      $this->redirect('itineraryReservation/edit?id='.$aoi_itinerary_reservation->getId());
    }
  }
}
